package moxl.parser.assemblers;

import moxl.parser.markup.Markup;
import moxl.reader.Location;
import moxl.tokenizer.AbstractTokenizerState;
import moxl.tokenizer.Token;
import moxl.tokenizer.TokenStream;
import moxl.tokenizer.TokenizerException;

public abstract class AbstractAssemblerContext<S>
{
	private final TokenStream tstream;

	private S state;
	private Token token;
	private Markup markup;

	public AbstractAssemblerContext(TokenStream tstream, S initialState)
	{
		this.tstream = tstream;
		this.state = initialState;
	}

	public S getState()
	{
		return state;
	}

	public void changeState(S newState)
	{
		this.state = newState;
	}

	public Token getToken()
	{
		return token;
	}

	public Location getLocation()
    {
        return getToken().getLocation();
    }

	public void setToken(Token token)
	{
		this.token = token;
	}

	public void setMarkup(Markup markup)
	{
		if (this.markup != null)
		{
			throw new IllegalStateException("Already have a result markup for this assembler");
		}
		this.markup = markup;
	}

	public void setTokenStreamMode(AbstractTokenizerState newState) throws TokenizerException
	{
		tstream.setMode(newState);
	}

	public boolean hasResult()
	{
		return markup != null;
	}

	public Markup getMarkup()
	{
		return markup;
	}

	public abstract boolean isNotFinished();

}
